package com.cortex.animation;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

public class CortexAnimatin {


	private View v;
	private int xTo,yTo, duration;
	private float fromX_Scale,fromY_Scale,toX_Scale,toY_Scale;
	public CortexAnimatin(View v, int xTo, int yTo, int duration,
			float fromX_Scale, float fromY_Scale, float toX_Scale,
			float toY_Scale) {
		super();
		this.v = v;
		this.xTo = xTo;
		this.yTo = yTo;
		this.duration = duration;
		this.fromX_Scale = fromX_Scale;
		this.fromY_Scale = fromY_Scale;
		this.toX_Scale = toX_Scale;
		this.toY_Scale = toY_Scale;
	}
	public View getView() {
		return v;
	}
	public void setView(View v) {
		this.v = v;
	}
	public int getX_To() {
		return xTo;
	}
	public void setX_To(int xTo) {
		this.xTo = xTo;
	}
	public int getY_To() {
		return yTo;
	}
	public void setY_To(int yTo) {
		this.yTo = yTo;
	}
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	public float getFromX_Scale() {
		return fromX_Scale;
	}
	public void setFromX_Scale(float fromX_Scale) {
		this.fromX_Scale = fromX_Scale;
	}
	public float getFromY_Scale() {
		return fromY_Scale;
	}
	public void setFromY_Scale(float fromY_Scale) {
		this.fromY_Scale = fromY_Scale;
	}
	public float getToX_Scale() {
		return toX_Scale;
	}
	public void setToX_Scale(float toX_Scale) {
		this.toX_Scale = toX_Scale;
	}
	public float getToY_Scale() {
		return toY_Scale;
	}
	public void setToY_Scale(float toY_Scale) {
		this.toY_Scale = toY_Scale;
	}
	public static AnimationInOut createAnimation(View view,Integer animationIn,Integer animationOut){
		return createAnimation(view, false, animationIn, animationOut);
	}
	public static AnimationInOut createAnimation(View view,boolean isShowEndAnimation,Integer animationIn,Integer animationOut){

		// create animation in and animation out
		View me = view;
		Context context = me.getContext();
		AnimationInOut animation = new AnimationInOut(view,isShowEndAnimation);
		if(animationIn != null){
			Animation in = loadAnimation(context,animationIn);
			animation.setIn(in);
			in.setAnimationListener(animation);

		}
		if(animationOut != null){
			Animation out = loadAnimation(context,animationOut);
			animation.setOut(out);
			out.setAnimationListener(animation);

		}
		return animation;
	}
	public static Animation loadAnimation(Context context,int resourceAnimation){
		Animation animation = AnimationUtils.loadAnimation(context, resourceAnimation);
		animation.setFillAfter(true);
		return animation;
	}
	public static TranslateAnimation createTranslate(int xTo, int yTo,int duration) {
		TranslateAnimation trans = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, xTo, TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, yTo);
		trans.setDuration(duration);
		
		return trans;
	}
	public static ScaleAnimation createScale(float fromX_Scale, float fromY_Scale, float toX_Scale, float toY_Scale,int duration) {
		ScaleAnimation scale = new ScaleAnimation(fromX_Scale, toX_Scale, fromY_Scale, toY_Scale);
		scale.setDuration(duration);
		return scale;
	}
	public static List<Animation> createTranslateWithScale(int xTo, int yTo,float fromX_Scale, float fromY_Scale, float toX_Scale, float toY_Scale,int duration) {
		List<Animation> animations = new ArrayList<Animation>();
		// create scale animation
		ScaleAnimation scale = new ScaleAnimation(fromX_Scale, toX_Scale, fromY_Scale, toY_Scale);
		scale.setDuration(duration);

		// create translation animation
		TranslateAnimation trans = createTranslate(xTo, yTo, duration);
		
		animations.add(scale);
		animations.add(trans);
		return animations;
	}
	public static List<Animation> createTranslateWithScale(CortexAnimatin animation) {
		return createTranslateWithScale(animation.getX_To(), animation.getY_To()
				, animation.getFromX_Scale(), animation.getFromY_Scale(), animation.getToX_Scale(), 
				animation.getToY_Scale(), animation.getDuration());
	}
	public static AlphaAnimation createAnimationAlpha(View v,float fromAlpha, float toAlpha,int duration) {

		AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
		alpha.setDuration(duration);
		return alpha;
	}
	public static List<Animation> createInOutAlpha(float fromAlpha, float toAlpha,int duration,int startOffset) {
		List<Animation> animations = new ArrayList<Animation>();
		AlphaAnimation alphaIn = new AlphaAnimation(fromAlpha, toAlpha);
		alphaIn.setDuration(duration);

		AlphaAnimation alphaOut = new AlphaAnimation(toAlpha,fromAlpha);
		alphaOut.setDuration(duration);
		alphaOut.setStartOffset(startOffset);
		animations.add(alphaIn);
		animations.add(alphaOut);
		return animations;
	}
	public static void startAnimations(View v,Animation...animations) {

		// create set of animations
		AnimationSet animationSet = new AnimationSet(false);
		// animations should be applied on the finish line
	
		for (Animation animation : animations) {
			animationSet.addAnimation(animation);
		}
		animationSet.setAnimationListener(new AnimationInOut(v));

		// start our animation
		v.startAnimation(animationSet);
	}


}
